package com.hyz.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hyz.model.IsComplete;
import com.hyz.model.ResponseResult;
import com.hyz.model.assetSecondment.AssetSecondment;
import com.hyz.model.assetSecondment.AssetSecondmentResult;
import com.hyz.model.assetSecondment.AssetSecondmentStatus;
import com.hyz.service.AssetSecondmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author HGD
 * @date 2022/10/9 20:17
 */
@Component
@Api("资产借调操作")
public class AssetSecondmentUtils {
    @Autowired
    private AssetSecondmentService asService;

    @ApiOperation("获取对应的资产借调申请")
    public ResponseResult<List<AssetSecondment>> getAssetSecondments(AssetSecondmentResult result) {
        QueryWrapper<AssetSecondment> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(AssetSecondment::getIsComplete, IsComplete.UNFINISHED)
                .eq(AssetSecondment::getResult, result)
                .eq(AssetSecondment::getStatus, AssetSecondmentStatus.TRANSFERS);
        List<AssetSecondment> list = asService.list(wrapper);
        if (list.isEmpty()) {
            return new ResponseResult<>(400, "无借调需求");
        }
        return new ResponseResult<>(200, "借调需求列表", list);
    }

    @ApiOperation("修改资产申")
    public ResponseResult<String> updateAssetSecondment(Long assetSecondmentId, AssetSecondmentResult beforeResult, AssetSecondmentResult result) {
        // 1. 判断对应的申请是否存在
        AssetSecondment assetSecondment = asService.getById(assetSecondmentId);
        if (Objects.isNull(assetSecondment)) {
            return new ResponseResult<>(400, "对应申请不存在");
        }

        // 2. 判断对应申请是否已完成
        // 3. 判断对应的申请类型是否为借用
        // 4. 判断对应的申请结果为 0
        if (assetSecondment.getIsComplete() != IsComplete.UNFINISHED ||
                assetSecondment.getStatus() != AssetSecondmentStatus.TRANSFERS ||
                assetSecondment.getResult() != beforeResult) {
            return new ResponseResult<>(400, "申请状态异常，请刷新");
        }

        // 5. 对申请结果进行修改
        IsComplete is = IsComplete.UNFINISHED;
        if (Objects.equals(result, AssetSecondmentResult.FAILED)) {
            is = IsComplete.COMPLETE;
        }
        UpdateWrapper<AssetSecondment> wrapper = new UpdateWrapper<>();
        wrapper.lambda().eq(AssetSecondment::getId, assetSecondmentId)
                .eq(AssetSecondment::getIsComplete, IsComplete.UNFINISHED)
                .set(AssetSecondment::getIsComplete, is)
                .eq(AssetSecondment::getStatus, AssetSecondmentStatus.TRANSFERS)
                .eq(AssetSecondment::getResult, beforeResult)
                .set(AssetSecondment::getResult, result);
        if (asService.update(wrapper)) {
            // 6. 返回所有本部门的资产借用申请
            return new ResponseResult<>(200, "审批成功");
        }

        return new ResponseResult<>(400, "审批错误，请重新选择");
    }
}
